package com.demo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.ResponseResult;
import com.demo.domain.entity.LogisticsSendinfo;
import com.demo.domain.vo.ConditionalPaginationVo;
import com.demo.domain.vo.LogisticInfovo;
import com.demo.domain.vo.PageVo;
import com.demo.mapper.PersonalInformationMapper;
import com.demo.service.LogisticsSendinfoService;
import com.demo.mapper.LogisticsSendinfoMapper;
import com.demo.util.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
*
*/
@Service
public class LogisticsSendinfoServiceImpl extends ServiceImpl<LogisticsSendinfoMapper, LogisticsSendinfo>
implements LogisticsSendinfoService{

    @Autowired
    private PersonalInformationMapper personalInformationMapper;

    @Override
    public ResponseResult viewLogistics(ConditionalPaginationVo ConditionalPaginationVo) {


        LambdaQueryWrapper<LogisticsSendinfo> queryWrapper = new LambdaQueryWrapper<>();

        //根据条件筛选需要显示的信息
        if (!Objects.equals(ConditionalPaginationVo.getSelect1(), "") && !Objects.equals(ConditionalPaginationVo.getInput3(), "")) {
            switch (ConditionalPaginationVo.getSelect1()) {
                case "1":
                    //寄件人手机号查询
                    queryWrapper.eq(LogisticsSendinfo::getSender, ConditionalPaginationVo.getInput3());
                    break;
                case "2":
                    //订单号查询
                    queryWrapper.eq(LogisticsSendinfo::getOrderNumber, ConditionalPaginationVo.getInput3());
                    break;
                case "3":
                    //收件人手机号查询
                    queryWrapper.eq(LogisticsSendinfo::getRecipient, ConditionalPaginationVo.getInput3());
                    break;
            }

        }

        if (ConditionalPaginationVo.isCheckAll()){
            //如果全部选择这不执行下面语句，否则执行
        }else if (ConditionalPaginationVo.getCheckedCities().size()>0 && !ConditionalPaginationVo.isCheckAll()) {
            //连接按订单状态查询，状态连接为或
            queryWrapper.and(e -> {
                for (int i=0;i<ConditionalPaginationVo.getCheckedCities().size();i++){
                    e.eq(LogisticsSendinfo::getOrderStatus, ConditionalPaginationVo.getCheckedCities().get(i)).or();
                }
            });

        }

        //对数据排序和分页
        queryWrapper.orderByDesc(LogisticsSendinfo::getOrderNumber);
        Page<LogisticsSendinfo> page = new Page(ConditionalPaginationVo.getPageNumber(), 6);
        page(page, queryWrapper);
        List<LogisticsSendinfo> logisticsSends = page.getRecords();
        List<LogisticInfovo> vs = BeanCopyUtils.copyBeanList(logisticsSends, LogisticInfovo.class);
        PageVo pageVo = new PageVo(vs, page.getTotal());
        return ResponseResult.okResult(pageVo);
    }
   /*
    @Override
    public ResponseResult viewLogistics(ConditionalPaginationVo ConditionalPaginationVo) {
        //查询物流人员所在区域
        QueryWrapper<PersonalInformation> personalInformationQueryWrapper = new QueryWrapper<>();
        personalInformationQueryWrapper.eq("account", SecurityUtils.getLoginUser().getUsername());
        PersonalInformation personalInformation = personalInformationMapper.selectOne(personalInformationQueryWrapper);



        LambdaQueryWrapper<LogisticsSendinfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(ew->{
            ew.and(e->{
                e.isNotNull(LogisticsSendinfo::getRouteArea)
                        .eq(LogisticsSendinfo::getRouteArea, personalInformation.getArea());
            });
            ew.or(e->{
                e.isNull(LogisticsSendinfo::getRouteArea).eq(LogisticsSendinfo::getSenderArea, personalInformation.getArea());
            });
        });


        //根据条件筛选需要显示的信息
        if (!Objects.equals(ConditionalPaginationVo.getSelect1(), "") && !Objects.equals(ConditionalPaginationVo.getInput3(), "")) {
            switch (ConditionalPaginationVo.getSelect1()) {
                case "1":
                    //寄件人手机号查询
                    queryWrapper.eq(LogisticsSendinfo::getSender, ConditionalPaginationVo.getInput3());
                    break;
                case "2":
                    //订单号查询
                    queryWrapper.eq(LogisticsSendinfo::getOrderNumber, ConditionalPaginationVo.getInput3());
                    break;
                case "3":
                    //收件人手机号查询
                    queryWrapper.eq(LogisticsSendinfo::getRecipient, ConditionalPaginationVo.getInput3());
                    break;
            }

        }

        if (ConditionalPaginationVo.isCheckAll()){
            //如果全部选择这不执行下面语句，否则执行
        }else if (ConditionalPaginationVo.getCheckedCities().size()>0 && !ConditionalPaginationVo.isCheckAll()) {
            //连接按订单状态查询，状态连接为或
            queryWrapper.and(e -> {
                for (int i=0;i<ConditionalPaginationVo.getCheckedCities().size();i++){
                    e.eq(LogisticsSendinfo::getOrderStatus, ConditionalPaginationVo.getCheckedCities().get(i)).or();
                }
            });

        }

        //对数据排序和分页
        queryWrapper.orderByDesc(LogisticsSendinfo::getOrderNumber);
        Page<LogisticsSendinfo> page = new Page(ConditionalPaginationVo.getPageNumber(), 6);
        page(page, queryWrapper);
        List<LogisticsSendinfo> logisticsSends = page.getRecords();
        List<LogisticInfovo> vs = BeanCopyUtils.copyBeanList(logisticsSends, LogisticInfovo.class);
        PageVo pageVo = new PageVo(vs, page.getTotal());
        return ResponseResult.okResult(pageVo);
    }
    */

}
